<?php

header('Cache-Control: private, no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');

/**
 * This script is used to remove message from Situare.
 *
 * Parameters:
 * [MANDATORY]  [mid]       [integer]   The message ID
 * 
 */
include_once './log4php/Logger.php';

include_once './config.php';
include_once './User.php';
include_once './Situare.php';
include_once './Error.php';
include_once './Utils.php';

// set up logging
Logger::configure('./log4j.properties');

$log = Logger::getLogger(basename(__FILE__));
$log->debug('[START] ' . basename(__FILE__));

try {
    $situare = new Situare();
    $inputArray = Utils::getInputParameters();
    $format = Utils::getInputParameterValue($inputArray, PARAM_FORMAT, FORMAT_TEXT);
    $api = Utils::getInputParameterValue($inputArray, PARAM_API, NULL);

    if (!$situare->isValidSession()) {
        $log->debug('$_COOKIE[] = ' . Utils::toString($_COOKIE));
        $log->debug('$_POST[] = ' . Utils::toString($_POST));
        $log->debug('$_GET[] = ' . Utils::toString($_GET));
        throw new SituareException(
                "Not found valid session data!",
                ERROR_SESSION_INVALID_OR_MISSING);
    }
	
    $mid = Utils::getInputParameterValue($inputArray, PARAM_MESSAGE_ID, NULL);
	
	if ($mid != NULL) {
		//Remove message from the Situare
		$situare->removeMessage($mid);
	}
	else {
        throw new Exception("Missing arguments!", ERROR_MISSING_ARGUMENT);
    }

    if ($api == API_2_0) {
        echo Situare::getApi20Response("Executed successfully!");
    }
    
    $log->debug('[ END ] ' . basename(__FILE__));
} catch (Exception $e) {
    $log->error("Exception:");
    $log->error("Code: " . $e->getCode());
    $log->error("Message: " . $e->getMessage());
    $log->error("Location: " . $e->getFile() . " at line " . $e->getLine());
    $log->error("Trace: " . $e->getTraceAsString());

    if ($api == API_2_0) {
        echo Situare::getApi20Response($e);
    }
    else {
        echo getExceptionAsJson($e);
    }
    
    exit();
}

?>

